package com.meida.module.bus.provider.handler;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusMaterial;
import com.meida.module.bus.client.entity.BusMaterialPackage100;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.service.BusMaterialService;
import com.meida.module.bus.provider.service.BusVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 100后台修正计划，变更计划添加用
 */
@Component("busMaterialPackage100SaveHandler")
public class BusMaterialPackage100SaveHandler implements SaveInterceptor<BusMaterialPackage100> {
    @Autowired
    private BusMaterialService busMaterialService;
    @Autowired
    private BusVersionService busVersionService;


    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap requestMap) {
        String levelNo = requestMap.get("levelNo");
        String packageNo = requestMap.get("packageNo");
        if (FlymeUtils.isEmpty(levelNo)) {
            return ResultBody.failed("层级号不能为空");
        } else {
            if (levelNo.length() < 11) {
                return ResultBody.failed("层级号长度不正确");
            }
        }
        if (!levelNo.substring(0, 5).equals(packageNo)) {
            return ResultBody.failed("层级号与工程包代码不一致");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusMaterialPackage100 busMaterialPackage100) {
        Long materialId = IdWorker.getId();
        busMaterialPackage100.setMaterialId(materialId);
        busMaterialPackage100.setMaterialPackage100Id(materialId);
        //默认0
        busMaterialPackage100.setParentId(0L);
        busMaterialPackage100.setTopParentId(materialId);
        Long versionId = IdWorker.getId();
        criteriaSave.put("materialId", materialId);
        criteriaSave.put("versionId", versionId);
        System.out.println("materialId1:" + materialId);
        busMaterialPackage100.setVersionId(versionId);
        busMaterialPackage100.setCreateTime(DateUtils.getNowDateTime());
        busMaterialPackage100.setStatus(1);
        busMaterialPackage100.setVersionStatus(1);
        busMaterialPackage100.setApplayStatus(0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusMaterialPackage100 entity) {
        EntityMap requestMap = cs.getRequestMap();
        Long projectId = requestMap.getLong("projectId");
        Long versionId = requestMap.getLong("versionId");
        BusVersion busVersion = new BusVersion();
        busVersion.setVersionId(versionId);
        busVersion.setProjectId(projectId);
        busVersion.setUserId(OpenHelper.getUserId());
        busVersion.setBigVersion("100");
        busVersion.setSmallVerion("00");
        busVersion.setState(1);//1正常 0锁定
        busVersion.setCreateTime(DateUtils.getNowDateTime());
        busVersionService.save(busVersion);
        Long materialId = requestMap.getLong("materialId");
        System.out.println("materialId2:" + materialId);
        BusMaterial busMaterial = cs.getEntity(BusMaterial.class);
        busMaterial.setMaterialId(materialId);
        busMaterial.setParentId(0L);
        busMaterial.setCreateTime(DateUtils.getNowDateTime());
        busMaterialService.save(busMaterial);
    }

}